<template>
  <el-dialog :title="'考试'" :visible.sync="visible"
             :close-on-click-modal="false" @close="hideDo">

    <el-form ref="form" :model="row" label-width="120px" align="left">
      
<el-form-item label="名称"
prop="name"
:rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
      <el-input v-model="row.name" placeholder="请输入"/>
</el-form-item>

<el-form-item label="开始时间">
<el-date-picker v-model="row.startTime"   type="datetime"       value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间"/>
</el-form-item>

<el-form-item label="结束时间">
<el-date-picker v-model="row.endTime"   type="datetime"       value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间"/>
</el-form-item>

<el-form-item label="试卷ID">
      <el-input v-model="row.paperId" placeholder="请输入"/>
</el-form-item>

<el-form-item label="试卷名称">
      <el-input v-model="row.paperName" placeholder="请输入"/>
</el-form-item>

<el-form-item label="考试时长">
      <el-input-number v-model="row.timeCount" placeholder="请输入"/>
</el-form-item>

<el-form-item label="题量">
      <el-input-number v-model="row.questionCount" placeholder="请输入"/>
</el-form-item>

<el-form-item label="总分">
      <el-input-number v-model="row.gradeCount" placeholder="请输入"/>
</el-form-item>

<el-form-item label="学生总数">
      <el-input-number v-model="row.userCount" placeholder="请输入"/>
</el-form-item>

<el-form-item label="考试类型">
<el-select v-model="row.examinationType">
<el-option
:label="'开卷'"
:value="0">
</el-option>
<el-option
:label="'闭卷'"
:value="1">
</el-option>
</el-select>
</el-form-item>

<el-form-item label="考试状态">
<el-select v-model="row.examinationStatus">
<el-option
:label="'待进行'"
:value="0">
</el-option>
<el-option
:label="'已结束'"
:value="1">
</el-option>
<el-option
:label="'考试中'"
:value="2">
</el-option>
</el-select>
</el-form-item>

<el-form-item label="老师ID">
      <el-input v-model="row.userId" placeholder="请输入"/>
</el-form-item>

<el-form-item label="老师昵称">
      <el-input v-model="row.nickName" placeholder="请输入"/>
</el-form-item>

<el-form-item label="发布">
<el-select v-model="row.isRelease">
<el-option
:label="'否'"
:value="0">
</el-option>
<el-option
:label="'是'"
:value="1">
</el-option>
</el-select>
</el-form-item>

<el-form-item label="备注">
      <el-input v-model="row.remark" placeholder="请输入"/>
</el-form-item>

<el-form-item label="学校ID">
      <el-input v-model="row.schoolId" placeholder="请输入"/>
</el-form-item>

<el-form-item label="状态">
<el-select v-model="row.isEnable">
<el-option
:label="'禁用'"
:value="0">
</el-option>
<el-option
:label="'启用'"
:value="1">
</el-option>
</el-select>
</el-form-item>


      <el-form-item>
        <el-button icon="el-icon-edit" type="primary" @click="submitForm">提交</el-button>
        <el-button icon="el-icon-delete" @click="hide()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>

<script>
  let data = () => {
    return {
      isAdd: false,
      visible: false,
      row: {},
      oldRow: {},
      isChange: false
    }
  }
  export default {
    data: data,
    props: ['search'],
    methods: {
      show: function (row) {
        this.oldRow = row;
        this.row = JSON.parse(JSON.stringify(row));
        this.visible = true;
        this.isChange = false;
      },
      showAdd: function () {
        this.isAdd = true;
        this.visible = true;
        this.oldRow = null;
        this.isChange = false;

        this.row = {
              examinationType: 1,
    examinationStatus: 1,
    isRelease: 0,
    isEnable: 1,

      }
      },
      hide: function () {
        this.visible = false;
      },
      hideDo: function () {
        this.isAdd = false;
        if (this.isChange && this.oldRow) {
          for (let item in this.oldRow) {
            let val = this.row[item];
            this.oldRow[item] = val;
          }
        }else{
          this.search();
        }
      },
      submitForm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.onSubmit();
          } else {
            console.log('valid error submit!!');
            return false;
          }
        });
      },
      onSubmit: function () {
        let param = this.row;
        let vm = this;
        let url = this.isAdd ? '/examinationInfoController/addExaminationInfo' : '/examinationInfoController/updateExaminationInfo';

        vm.$http.post(url, param).then(res => {
          if (!res.data.success) {
            vm.$message({
              showClose: true,
              message: res.data.msg,
              type: 'error'
            });
            return
          }
          vm.isChange = true;
          vm.$message({
            type: 'success',
            message: '保存成功!'
          })
          vm.hide()
        })
      }
    },
    name: "ExaminationInfoEdit"
  }
</script>

<style scoped>

</style>

